package com.example.mannortycoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mannortycoon.dao.UserRelationShipDao;
import com.example.mannortycoon.entity.UserEntity;
import com.example.mannortycoon.entity.UserRelationShipEntity;
import com.example.mannortycoon.service.UserRelationShipService;
import com.example.mannortycoon.service.UserService;
import com.example.mannortycoon.uitl.InvitationCodeUtil;
import com.example.mannortycoon.uitl.R;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserRelationShipServiceImpl extends ServiceImpl<UserRelationShipDao, UserRelationShipEntity> implements UserRelationShipService {

    private final UserService userService;

    public UserRelationShipServiceImpl(UserService userService){
        this.userService = userService;
    }

    @Override
    public R addRelationShip(Long userId, String code) {
        Long userId2 = InvitationCodeUtil.decode(code);
        UserEntity byId = userService.getById(userId2);
        if(byId == null){
            return R.error("邀请码有误！");
        }
        return this.addRelationShip(userId,userId2);
    }

    @Override
    public R addRelationShip(Long userId, Long userId2) {
        if(userId.equals(userId2)){
            return R.error("不能添加自己");
        }
        //查找好友关系，查看是否已绑定
        QueryWrapper<UserRelationShipEntity> userRelationShipEntityQueryWrapper = new QueryWrapper<>();
        userRelationShipEntityQueryWrapper.eq("user1_id",userId).eq("user2_id",userId2);
        UserRelationShipEntity one = this.getOne(userRelationShipEntityQueryWrapper);
        if(one != null){
            return R.error("已经是好友关系了");
        }
        UserRelationShipEntity userRelationShipEntity = new UserRelationShipEntity();
        userRelationShipEntity.setUser1Id(userId2);
        userRelationShipEntity.setUser2Id(userId);
        userRelationShipEntity.setCreateTime(new Date());
        UserRelationShipEntity userRelationShipEntity1 = new UserRelationShipEntity();
        userRelationShipEntity1.setUser1Id(userId);
        userRelationShipEntity1.setUser2Id(userId2);
        userRelationShipEntity1.setCreateTime(new Date());
        List <UserRelationShipEntity> list = new ArrayList<>();
        list.add(userRelationShipEntity1);
        list.add(userRelationShipEntity);
        this.saveBatch(list);
        return R.ok();
    }

    @Override
    public List<UserEntity> getRelationShipList(Long userId) {
        QueryWrapper<UserRelationShipEntity> userRelationShipEntityQueryWrapper = new QueryWrapper<>();
        userRelationShipEntityQueryWrapper.eq("user1_id",userId);
        List<UserRelationShipEntity> list = this.list(userRelationShipEntityQueryWrapper);
        List<Long> collect = list.stream().map(UserRelationShipEntity::getUser2Id).collect(Collectors.toList());
        return userService.getUserInfoListByIds(collect);
    }

    @Override
    public R givePearl(Long userId1, String username, Double pearl) {
        //获取用户1信息
        UserEntity userEntity = userService.getById(userId1);
        if(userEntity == null){
            return R.error("登录信息有误");
        }
        //获取用户2信息
        //根据用户名获取用户
        UserEntity userEntity2 = userService.getByUsername(username);
        if(userEntity2 == null){
            return R.error("该用户不存在");
        }
        if(userEntity.getPearl() < pearl){
            return R.error("用户珍珠不足");
        }
        userEntity.setPearl(userEntity.getPearl() - pearl);
        userEntity2.setPearl(userEntity2.getPearl() + pearl);
        List<UserEntity> userEntityList = new ArrayList<>();
        userEntityList.add(userEntity);
        userEntityList.add(userEntity2);
        userService.updateBatchById(userEntityList);
        return R.ok();
    }
}
